package com.quwan.manager.service.impl;

import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.config.yml.OssConfig;
import com.quwan.constans.CacheKey;
import com.quwan.entity.TMemberLevel;
import com.quwan.manager.service.MemberLevelService;
import com.quwan.mapper.TMemberLevelMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author laozhang
 * @date 2021/11/17 17:21
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheKey.DATA.member_level)
public class MemberLevelServiceImpl implements MemberLevelService {
    private final TMemberLevelMapper memberLevelMapper;
    private final OssConfig ossConfig;

    @Cacheable
    @Override
    public List<TMemberLevel> getMemberLevelList() {
        LambdaQueryWrapperX<TMemberLevel> wrapper = new LambdaQueryWrapperX<TMemberLevel>();
        wrapper.orderByAsc(TMemberLevel::getMLevel);
        List<TMemberLevel> resList = memberLevelMapper.selectList(wrapper);
        /* 开始遍历 */
        for (TMemberLevel ban : resList) {
            if(ban.getIcon()!= null && ban.getIcon().length()>0){
                ban.setIcon(ossConfig.getHostname()+"/"+ban.getIcon());
            }
        }
        return resList;
    }

    @CacheEvict(cacheNames = CacheKey.DATA.member_level, allEntries = true, beforeInvocation = true)
    @Override
    public boolean addMemberLevel(TMemberLevel memberLevel) {
        return memberLevelMapper.insert(memberLevel) == 1;
    }

    @CacheEvict(cacheNames = CacheKey.DATA.member_level, allEntries = true, beforeInvocation = true)
    @Override
    public boolean updateMemberLevel(TMemberLevel memberLevel) {
        if (StringUtils.isBlank(memberLevel.getIcon()))
            memberLevel.setIcon(null);
        return memberLevelMapper.updateById(memberLevel) == 1;
    }

    @CacheEvict(cacheNames = CacheKey.DATA.member_level, allEntries = true, beforeInvocation = true)
    @Override
    public boolean delMemberLevel(Integer id) {
        return memberLevelMapper.deleteById(id) == 1;
    }
}
